<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="Please input user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="Please input password"
            show-password
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item>
          <el-button type="primary" @click="submitLoginForm('loginFormRef')"
            >登录</el-button
          >
          <el-button type="info" @click="resetLoginForm('loginFormRef')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, getCurrentInstance, inject } from "vue";
import { useRouter } from "vue-router";

export default {
  setup() {
    // 获取当前实例 ctx类似于this
    const { proxy } = getCurrentInstance();
    // 获取路由
    const router = useRouter();
    // 注入一个axios
    const axios = inject("axios");

    // 登录表单的数据绑定对象
    const loginForm = reactive({
      username: "admin",
      password: "123456",
    });

    // 表单的验证规则对象
    const loginFormRules = reactive({
      // 验证用户名是否规范
      username: [
        {
          required: true,
          message: "Please input Activity username",
          trigger: "blur",
        },
        {
          min: 3,
          max: 5,
          message: "Length should be 3 to 5",
          trigger: "blur",
        },
      ],
      // 验证密码是否合法
      password: [
        {
          required: true,
          message: "Please input Activity password",
          trigger: "blur",
        },
        {
          min: 6,
          max: 15,
          message: "Length should be 6 to 15",
          trigger: "blur",
        },
      ],
    });

    // 重置按钮
    const resetLoginForm = (loginFormRef) => {
      proxy.$refs[loginFormRef].resetFields();
      loginForm.username = "";
      loginForm.password = "";
    };

    // 登录按钮
    const submitLoginForm = (loginFormRef) => {
      // 登录预验证
      proxy.$refs[loginFormRef].validate(async (valid) => {
        if (!valid) return;

        // 发送请求获取结果
        const { data: res } = await axios.post("login", proxy.loginForm);

        // 失败和成功后的处理
        if (res.meta.status !== 200) return proxy.$message.error("登录失败");
        proxy.$message.success("登录成功");

        // 在这里存储一个用于判断用户是否登录（有些业务只能用户登录以后才能访问）
        sessionStorage.setItem("token", res.data.token);

        // 路由跳转
        router.push("/home");
      });
    };

    return {
      loginForm,
      loginFormRules,
      resetLoginForm,
      submitLoginForm,
    };
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #ccc;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>